using System;
using System.ComponentModel;

namespace SharpMud.Server.Systems.Security
{
    [Browsable(false)]
    public class SecurityManager : IServerSystem
	{
		#region Private Variables
		private SharpMud.Server.App.Application _HostApp;
        private ServerSystemStatus              __Status=ServerSystemStatus.Uninitialized;
		#endregion

		#region Constructors
		public SecurityManager()
		{
			// System.Threading.Thread.CurrentPrincipal = SharpMud System
		}
		public SecurityManager(SharpMud.Server.App.Application hostApp)
		{
			this.Initialize(hostApp);
		}
		#endregion

		#region Static Globalization
		private static SecurityManager _GlobalInstance;
		public static SecurityManager GlobalInstance
		{
			get
			{
				return _GlobalInstance;
			}
		}
		public void GoGlobal()
		{
			if(_GlobalInstance!=null)
				throw new InvalidOperationException("There is already a global instance of SharpMud.Server.Systems.SecurityManager");
			_GlobalInstance = this;
		}
		public void RetractGlobal()
		{
            if (_GlobalInstance == null)
                return;
			if(_GlobalInstance!=this)
				throw new InvalidOperationException("The currently global instance of SharpMud.Server.Systems.SecurityManager is not this instance");
			_GlobalInstance = null;
		}
		#endregion

		#region Public Properties
		public SharpMud.Server.App.Application HostApp
		{
			get
			{
				return _HostApp;
			}
		}
		#endregion

		public void Initialize(SharpMud.Server.App.Application hostApp)
		{
            this._Status = ServerSystemStatus.Initializing;

            if(this._HostApp != null)
				throw new InvalidOperationException();

			_HostApp = hostApp;

            this._Status = ServerSystemStatus.Initialized;
		}

        protected ServerSystemStatus _Status
        {
            get
            {
                return __Status;
            }
            set
            {
                if (value == this.__Status)
                    return;
                
                ServerSystemStatus oldStatus = this.__Status;
                this.__Status = value;
                
                if (this.StatusChanged != null)
                    this.StatusChanged(this, new ServerSystemStatusChangeEventArgs(oldStatus, value));
            }
        }


		internal string GenerateSecureAppKey()
		{
			System.Security.Cryptography.HashAlgorithm hasher;
			hasher = new System.Security.Cryptography.SHA512Managed();
			
			string SuperSecretKey = Utility.InterlaceStrings(_HostApp.___AppID.ToString(),"SharpMud:Beta:SecureKey:Salt");
			string HashedKey = Utility.DefaultTextEncoding.GetString(hasher.ComputeHash(Utility.DefaultTextEncoding.GetBytes(SuperSecretKey)));
			return HashedKey;
		}

		internal string GetSecureAppKey()
		{
			return _HostApp.___SecureAppKey;
		}

        #region IServerSystem Members

        public void Initialize(IServerSystemHost host)
        {
            this.Initialize(host as SharpMud.Server.App.Application);
        }

        public void Start()
        {
            return;
        }

        public void Pause()
        {
            return;
        }

        public void Resume()
        {
            return;
        }

        public void Stop()
        {
            return;
        }

        public void Dispose()
        {
            return;
        }

        public ServerSystemStatus Status
        {
            get { return _Status; }
        }

        public event EventHandler<ServerSystemStatusChangeEventArgs> StatusChanged;

        public IServerSystemHost Host
        {
            get { return (this._HostApp as IServerSystemHost); }
        }

        #endregion
    }
}
